# encoding: utf-8

import requests,json
import os,time,sys

import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 第三方 SMTP 服务
mail_host = "smtp.qq.com"  # 设置服务器
mail_user = "910325476@qq.com"  # 用户名
mail_pass = "jirfmwwwjfgybfhj"  # 口令

sender = '910325476@qq.com'
receivers = ['kuan.zhou@awit.net.cn']  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱

cnt=[]


#API
METEO_API='http://meteo.awit.net.cn:10280/GuiYang_weather_BS/ncTask.do?doNcTask'
headers  = {'Content-Type': 'application/json;charset=UTF-8'}
#Nc存放的路径
FILE_PATH="/mnt/ftp2/ncfiles/wrf_{0}"
FILE_OUT_PATH='/mnt/ftp2/awitNcfiles/wrf_{0}'

# FILE_PATH="F:/Share/awitNcFile/in/wrf_{0}"
# FILE_OUT_PATH='F:/Share/awitNcFile/out/wrf_{0}'

#缓存大小
CHUNKSIZE=512*1024

#下载的配置
DATA_CONFIG={
    'lsw2':{
        'name':'鲁苏皖-1Km',
        'fn':'china_long_lusuwan_d2.nc',
        'api':'http://183.57.41.102/wrf_lusuwan/nc/china_lusuwan_{0}_d2.nc',
        'check':'http://183.57.41.102/wrf_lusuwan/nc/china_lusuwan_api.php?lon=117.151552&lat=31.857605&model={0}&domain=d2',
        'plan':['A'],
        'update':True
    },
    'lsw1':{
        'name':'鲁苏皖-3Km',
        'fn':'china_long_lusuwan_d1.nc',
        'api':'http://183.57.41.102/wrf_lusuwan/nc/china_lusuwan_{0}_d1.nc',
        'check':'http://183.57.41.102/wrf_lusuwan/nc/china_lusuwan_api.php?lon=117.151552&lat=31.857605&model={0}&domain=d1',
        'plan':['A'],
        'update':True
    },
    'yq':{
        'name':'渝黔-3',
        'fn':'china_long.nc',
        'api':'http://113.106.88.29/wrf_yuqian/nc/china_yuqian_{0}_d1.nc',
        'check':'http://113.106.88.29/wrf_yuqian/nc/china_yuqian_api.php?lon=106.703449&lat=29.772927&model={0}&domain=d1',
        'plan':['A','B','C'],
        'update':True
    }
}

#报警
def warn(code,level,msg):
    global cnt
    if level==0:
        cnt.append('[{0}]:超算服务器api获取数据为空,api:{1}'.format(code,msg))
        print('[{0}]:超算服务器api获取数据为空,api:{1}'.format(code,msg))
    elif level==1:
        cnt.append('[{0}]:超过12h数据未更新'.format(code))
        print('[{0}]:超过12h数据未更新'.format(code))
    elif level==9:
        cnt.append('[{0}]:api异常,[{1}]'.format(code,msg))
        print('[{0}]:api异常,[{1}]'.format(code,msg))

def sendSmtp():
    msg=''
    for line in cnt:
        msg+=line+'\n'
    message = MIMEText(msg, 'plain', 'utf-8')
    message['From'] = Header("Meteo数据服务器", 'utf-8')
    message['To'] = Header("提醒", 'utf-8')
    subject = '服务器Nc文件数据异常'
    message['Subject'] = Header(subject, 'utf-8')
    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, 465)
        smtpObj.set_debuglevel(1)
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(sender, receivers, message.as_string())
        print "邮件发送成功"
    except smtplib.SMTPException:
        print "Error: 无法发送邮件"


#检查nc是否需要更新
def checkData(modelCode,checkApi,plan):
    checkData = requests.get(checkApi.format(plan))
    if checkData.status_code==200:
        apiData = json.loads(checkData.text)
        if len(apiData)==0:
            warn(modelCode,0,checkApi.format(plan))
            return False
        else:
            ncOutPath=FILE_OUT_PATH.format(plan)
            if os.path.exists(ncOutPath) is False:
                os.makedirs(ncOutPath)
                return True
            endtime = apiData[len(apiData) - 1]['time']  # 数据的结束时间
            for f in os.listdir(ncOutPath):
                if f.endswith('.nc') == False :
                    continue
                split = f.replace('.nc', '').split('_')
                if split[1]!=modelCode:
                    continue
                lastTime=split[2]
                if lastTime==endtime:
                    return False
            return True
    else:
        warn(modelCode,9,checkApi.format(plan))
        return False

#下载nc
def downloadNc(code,down,save):
    try:
        r = requests.get(down, stream=True)
        total_size = int(r.headers['Content-Length'])
        if total_size < 1024:
            warn(code, 9, '下载1:' + down)
            raise Exception
        f = open(save, "wb")
        temp_size = 0
        for chunk in r.iter_content(chunk_size=CHUNKSIZE):
            if chunk:
                temp_size += len(chunk)
                f.write(chunk)
                f.flush()
                sys.stdout.write(
                    '\r{0}/{1}(Mb) {2}% [{3}] '.format( temp_size / 1024 / 1024,
                                                                total_size / 1024 / 1024, 100 * temp_size / total_size,
                                                                save))
                sys.stdout.flush()
    except:
        warn(code, 9, '下载2:'+down)
    pass

def meteoInPut(files):
    r = requests.post(METEO_API, data=json.dumps(files), headers=headers)
    print(files)

if __name__=='__main__':
    filename=[]
    for modelCode in DATA_CONFIG:
        modelCfg=DATA_CONFIG[modelCode]
        if modelCfg['update']:
            for pl in modelCfg['plan']:
                if checkData(modelCode,modelCfg['check'],pl):
                    downloadNc(modelCode,modelCfg['api'].format(pl),FILE_PATH.format(pl)+'/'+modelCfg['fn'])
                    filename.append({'p':pl,'fn':modelCfg['fn']})
                else:
                    # 判断数据是否未更新
                    ncInPath = FILE_PATH.format(pl) + '/' + modelCfg['fn']
                    if os.path.exists(ncInPath) and (time.time() - os.stat(ncInPath).st_mtime) > 12 * 60 * 60:
                        warn(modelCode,1)
    if(len(cnt)>0):
        sendSmtp()
    meteoInPut(filename)
